/*****************************************************
*
*               COPYRIGHT (c) 2017-2019 Hargic Corporation
*                         All Rights Reserved
*
* The source code contained or described herein and all documents
* related to the source code ("Material") are owned by Hargic
* Corporation or its licensors.  Title to the Material remains
* with Hargic Corporation or its suppliers and licensors.
*
* The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used,
* copied, reproduced, modified, published, uploaded, posted, transmitted,
* distributed, or disclosed in any way except in accordance with the
* applicable license agreement.
*
* No license under any patent, copyright, trade secret or other
* intellectual property right is granted to or conferred upon you by
* disclosure or delivery of the Materials, either expressly, by
* implication, inducement, estoppel, except in accordance with the
* applicable license agreement.
*
* Unless otherwise agreed by Hargic in writing, you may not remove or
* alter this notice or any other notice embedded in Materials by Hargic
* or Hargic's suppliers or licensors in any way.
*
** Author: shengda.huang
** Date: 2019-04-07
*
*****************************************************/

/***** Include files ********************************/
#include <bcm2835.h>
#include <stdio.h>

/***** Defines **************************************/

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

/***** Define Prototype of functions ****************/

/**
 * @brief  raspberry i2c clock divided is based on nominal base clock rate of 250MHz
 * @param  None
 * @param  None
 * @param  clkdiv:  BCM2835_I2C_CLOCK_DIVIDER_2500   2500 = 10us = 100 kHz
                    BCM2835_I2C_CLOCK_DIVIDER_626     622 = 2.504us = 399.3610 kHz
                    BCM2835_I2C_CLOCK_DIVIDER_150     150 = 60ns = 1.666 MHz (default at reset)
                    BCM2835_I2C_CLOCK_DIVIDER_148     148 = 59ns = 1.689 MHz
 * @retval None
 */
int i2c_init(unsigned char slv_addr, int rate, int clkdiv)
{
    int ret;

    ret = bcm2835_i2c_begin();
    printf("%s ret %d\n", "bcm2835_i2c_begin", ret);
    bcm2835_i2c_setSlaveAddress(slv_addr);
    // bcm2835_i2c_set_baudrate(rate);
    bcm2835_i2c_setClockDivider(clkdiv);
    printf("i2c slv_addr 0x%x rate %d\n", slv_addr, rate);

    return 0;
}

int i2c_deinit(void)
{
    bcm2835_i2c_end();
    return 0;
}

int i2c_read(unsigned char *pbuf, int len)
{
    return bcm2835_i2c_read((char *)pbuf, len);
}

int i2c_write(unsigned char *pbuf, int len)
{
    return bcm2835_i2c_write((char *)pbuf, len);
}

int i2c_write_read(unsigned char *wr, int wrlen, unsigned char *rd, int rdlen)
{
    return bcm2835_i2c_write_read_rs((char *)wr, wrlen, (char *)rd, rdlen);
}

/* End of file */



